<?php
/**
 * Created by PhpStorm.
 * User: angl
 * Date: 2016/9/28
 * Time: 19:19
 */
namespace AuctionCore\AntNest;

use AuctionCore\Common\Request;

class Client
{
    protected $urlPath = '';
    protected $params = array();
    protected $is_post = 1;


    /**
     * 发送请求
     * @return string
     */
    public function send()
    {
        $re = new Request();
        $params = json_encode($this->params, JSON_UNESCAPED_UNICODE);
        $result = $re->juHeCurl($this->urlPath, $params, $this->is_post);
        if ($result['code'] !== '000000') {
            \Think\Log::record(json_encode($result), 'DEBUG', true);
            return false;
        }
        return true;
    }

    /**
     * 获取数据
     * @return bool
     */
    public function getData()
    {
        $re = new Request();
        $params = json_encode($this->params, JSON_UNESCAPED_UNICODE);
        $result = $re->juHeCurl($this->urlPath, $params, $this->is_post);
        if ($result['code'] !== '000000') {
            $this->formatResult($result['code'], $result['msg']);
            \Think\Log::record($result['code'] . " : " . $result['msg'], 'ERROR', true);
            return false;
        }
        return $result['data'];
    }

    /**
     * 格式输出
     * @param $code
     * @param $msg
     * @param string $show
     */
    public function formatResult($code, $msg, $show = "ajax")
    {
        if ($show == 'ajax') {
            echo json_encode(['code' => $code, 'msg' => $msg]);
            exit;
        }
    }
}
